/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ages.sound

import java.nio.ByteBuffer
import net.java.games.joal.ALConstants
import net.java.games.joal.ALException
import net.java.games.joal.ALFactory
import net.java.games.joal.util.ALut

object ALLoader{

  val NUM_SOURCES = 30
//  val NUM_BUFFERS = 30
  private var nextSource = 0

  ALut.alutInit();
  val al = ALFactory.getAL();
  al.alGetError();

  val source = new Array[Int](NUM_SOURCES);
   // Bind buffer with a source.
    al.alGenSources(NUM_SOURCES, source, 0);
    
  def load(filename:String):Array[Int]={
  val buffer = new Array[Int](1);
    // variables to load into

    val format:Array[Int] = new Array[Int](1);
    val size:Array[Int] = new Array[Int](1);
    val freq:Array[Int] = new Array[Int](1);
    val loop:Array[Int] = new Array[Int](1);
    val data:Array[ByteBuffer] = new Array[ByteBuffer](1)

    al.alGenBuffers(1, buffer, 0);
    if (al.alGetError() != ALConstants.AL_NO_ERROR)
      throw new ALException("Error generating OpenAL buffers");
val file = getClass().getResourceAsStream(filename)
//println(file)
    ALut.alutLoadWAVFile(file,
      format,
      data,
      size,
      freq,
      loop);
    if (data(0) == null) {
      throw new RuntimeException("Error loading WAV file");
    }
//    System.out.println("sound size = " + size(0));
//    System.out.println("sound freq = " + freq(0));
    al.alBufferData(buffer(0), format(0), data(0), size(0), freq(0));

 

    if (al.alGetError() != ALConstants.AL_NO_ERROR)
      throw new ALException("Error generating OpenAL source")

//    al.alSourcei(source(0), ALConstants.AL_BUFFER, buffer(0))
//    al.alSourcef(source(0), ALConstants.AL_PITCH, 1.0f)
//    al.alSourcef(source(0), ALConstants.AL_GAIN, 1.0f)
//    al.alSourcei(source(0), ALConstants.AL_LOOPING, loop(0))

    // Do another error check
    if (al.alGetError() != ALConstants.AL_NO_ERROR)
      throw new ALException("Error setting up OpenAL source")

    // Note: for some reason the following two calls are producing an
    // error on one machine with NVidia's OpenAL implementation. This
    // appears to be harmless, so just continue past the error if one
    // occurs.
//    al.alSourcefv(source(0), ALConstants.AL_POSITION, sourcePos, 0);
//    al.alSourcefv(source(0), ALConstants.AL_VELOCITY, sourceVel, 0);
    buffer
  }

    def main(args:Array[String])
  {
//    load("/com/ages/test/res/bee.wav")
//    al.alSourcePlay(source(0))
    Thread.sleep(1000)
    while(true)
      {
    val s = new Sound("/com/ages/test/res/waterdrip.wav")
    SoundBufferManager.bufferSoundQueue
    s.play
    val wait = 500-(nextSource*12)
    println(wait)
    Thread.sleep(wait)
      }
  }

  def playSound(buffer:Array[Int],gain:Float,pitch:Float,loop:Boolean)
  {

    al.alSourcei(source(nextSource), ALConstants.AL_BUFFER, buffer(0))
    al.alSourcef(source(nextSource), ALConstants.AL_PITCH, pitch)
    al.alSourcef(source(nextSource), ALConstants.AL_GAIN, gain)
    al.alSourcei(source(nextSource), ALConstants.AL_LOOPING, if(loop) ALConstants.AL_TRUE else ALConstants.AL_FALSE)


    al.alSourcePlay(source(nextSource))
    nextSource+=1
    if(nextSource>=NUM_SOURCES)
      nextSource=0
  }
}
